package com.作业.Day22;

import lombok.Data;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;


public class MyJDBC {
   static Connection connection = null;
   private static String className;
   private static String classPath;
   private static String root;
   private static String password;

    public MyJDBC() {
    }

    /**
     *    初始化时，进行驱动
     *
     *
     */
   static {
       //获取配置文件信息
       Properties properties = new Properties();
       //获取配置文件的输入流
       FileReader fileReader = null;
       try {
           fileReader = new FileReader("homework1\\src\\main\\resources\\jdbc.properties");
       } catch (FileNotFoundException e) {
           throw new RuntimeException(e);
       }
       try {
           properties.load(fileReader);
       } catch (IOException e) {
           throw new RuntimeException(e);
       }
        className = (String) properties.get("className");
        classPath = (String) properties.get("classPath");
        root = (String) properties.get("root");
        password = (String) properties.get("password");
        System.out.println("className: " + className);
        System.out.println("classPath: " + classPath);
        System.out.println("root: " + root);
        System.out.println("password: " + password);
       //获取后，进行驱动
       try {
           Class.forName(className);
       } catch (ClassNotFoundException e) {
           throw new RuntimeException(e);
       }
   }

    /**
     * 判断是否连接数据库成功
     *
     * @return
     */
     public static Connection isConnect(){
         try {
             if(connection == null|| !connection.isClosed()){
                 connection = DriverManager.getConnection(classPath, root, password);
             }
         } catch (SQLException e) {
             System.out.println("判断失败");
             throw new RuntimeException(e);
         }
         System.out.println("连接成功");
         return connection;

     }
   //修改操作,返回影响的行数
    static public int updateDB(String sql,Object... args)  {
        Connection connect = isConnect();
        PreparedStatement preparedStatement = null;
        try {
             preparedStatement = connect.prepareStatement(sql);
              //进行参数判断。然后在传递参数
            if(args!=null&&args.length>0){
                for (int i = 0; i < args.length; i++) {
                    preparedStatement.setObject(i+1,args[i]);
                }
                int i1 = preparedStatement.executeUpdate();
                  return i1;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        finally {
            //关闭资源
                close(connect,preparedStatement,null);
        }
        return 0;
    }
    //资源关闭
    static public void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
        try {
            if(connection!=null){
                connection.close();
            }
            if(preparedStatement!=null){
                preparedStatement.close();
            }
            if(resultSet!=null){
                resultSet.close();
            }
        } catch (SQLException e) {
            System.out.println("关闭失败");
            e.printStackTrace();
        }
    }
    //查询所有信息
    static public <T> void queryDB(String sql, ResultSetHandler<T> handler) {
        try {
            ArrayList<T> ts = new ArrayList<>();
            Connection connect = isConnect();
            PreparedStatement preparedStatement = connect.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();

            int i=1;
            while (resultSet.next()) {
                T item = handler.handle(resultSet);
                ts.add(item);
            }
            System.out.println(ts);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
// 回调接口
interface  ResultSetHandler<T> {
    T handle(ResultSet rs) throws SQLException;
}
